/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('HotelListFilterCtrl', ['$scope','$rootScope', '$timeout', '$state', 'FilterService', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $state, FilterService, ionicMaterialMotion, ionicMaterialInk) {
    var type = $state.params.type,
        loadDist = function(){
            if (type === 'chars-visit') {
                $scope.filter.isCharsVisit = true;
                $scope.filter.charsVisit = [];
                $scope.filter.groups = [];
                FilterService.findDictByCode('visit_type')
                    .success(function (res) {
                        if (res.success && res.attribute) {
                            var obj = res.attribute;
                            var charsVisitList = [];
                            for (var i in obj) {
                                charsVisitList.push({
                                    id: i,
                                    name: obj[i]
                                });
                            }
                            var item = {
                                id: 'charsVisitKey',
                                name: '拜访类型',
                                items: charsVisitList
                            };
                            $scope.filter.charsVisit[0] = item;
                            $rootScope.CHARS_VISIT_DAYS = {
                                id: 'visitDay',
                                name: '拜访日期',
                                items: [{
                                    id: 1,
                                    value: 'all',
                                    name: '全部'
                                }, {
                                    id: 2,
                                    value: '-6',
                                    name: '近七天'
                                }, {
                                    id: 3,
                                    value: '-2',
                                    name: '近三天'
                                }, {
                                    id: 4,
                                    value: '0',
                                    name: '今天'
                                }, {
                                    id: 5,
                                    value: '-1',
                                    name: '昨天'
                                }]
                            };
                            $scope.filter.charsVisit[1] = $rootScope.CHARS_VISIT_DAYS;
                        }
                    })
                    .error(function () {
                        console.log('没有最新数据');
                    })
                    .finally(function () {

                    });
            } else {
                FilterService.findList()
                    .success(function(res){
                        if(res.success && res.attribute.directList){
                            var _dataList = res.attribute.directList;
                            $scope.filter.groups[$scope.filter.groups.length-1].items = _dataList;
                        }else{
                            console.log('没有最新数据');
                        }
                    }).error(function(error){
                    console.log(error);
                }).finally(function(){
                });
            }
        },
        hiddenExp = [{
            id:'hotel-list-private-danger',
            items:['is_healthy']
        },{
            id:'hotel-list-private-swath',
            items:['sffxhz']
        },{
            id:'hotel-list-private-wash',
            items:['sfxdhz']
        }],
        format = function(){
            var _exp = _.findWhere(hiddenExp,{'id':type});
            if(_exp){
                angular.forEach(_exp.items,function(item){
                    var _filter = _.findWhere($scope.filter.groups,{'id':item});
                    if(_filter){
                        _filter.display = 'none';
                    }
                });
            }
        },
        init = function(){
            format();
            loadDist();
        };
    $scope.title =  '筛选';

    /*
     * if given group is the selected group, deselect it
     * else, select the given group
     */
    $scope.toggleGroup = function(group) {
        if ($scope.isGroupShown(group)) {
            $scope.shownGroup = null;
        } else {
            $scope.shownGroup = group;
        }
    };
    $scope.isGroupShown = function(group) {
        return $scope.shownGroup === group;
    };

    $scope.doAct = function(){
        var url = 'app.hotel-list-public';
        if(!$rootScope.FILTER_DATE)
            $rootScope.FILTER_DATE = {};
        $rootScope.FILTER_DATE[type] = {
            backData:$scope.filter.selected
        };
        $rootScope.$broadcast(type, $scope.filter.selected);
        $scope.$ionicGoBack();
    };
    $scope.doClear = function(){
        $scope.filter.selected = {};
    };
    $scope.filter = {
        getName:function(groupId){
            var selectId = this.selected[groupId],
                i,
                j,
                group,
                item;
            for( i in this.groups){
                 group = this.groups[i],
                    item = {};
                if(group.id == groupId){
                    for( j in group.items){
                        item = group.items[j];
                        if(item.id == selectId){
                            return item.name;
                        }
                    }
                }
            }
            return '全部';
        },
        getVisitName:function(groupId){
            var selectId = this.selected[groupId],
                i,
                j,
                charsVisit,
                item;
            for( i in this.charsVisit){
                charsVisit = this.charsVisit[i],
                    item = {};
                if(charsVisit.id == groupId){
                    for( j in charsVisit.items){
                        item = charsVisit.items[j];
                        if(item.id == selectId){
                            return item.name;
                        }
                    }
                }
            }
            return '全部';
        },
        selected:$rootScope.FILTER_DATE && $rootScope.FILTER_DATE[type] ? $rootScope.FILTER_DATE[type].backData : {},
        groups:[{
            id:'hotelRank',
            name: '评级级别',
            items: [{
                id:'A',
                name:'A级'
            },{
                id:'B',
                name:'B级'
            },{
                id:'C',
                name:'C级'
            }]
        },{
            id:'is_healthy',
            name: '健康程度',
            items: [{
                id:1,
                name:'健康'
            },{
                id:2,
                name:'一般'
            },{
                id:3,
                name:'危险'
            }]
        },{
            id:'sffxhz',
            name: '是否分销合作',
            items: [{
                id:1,
                name:'是'
            },{
                id:0,
                name:'否'
            }]
        },{
            id:'sfgylhz',
            name: '是否采购合作',
            items: [{
                id:1,
                name:'是'
            },{
                id:0,
                name:'否'
            }]
        },{
            id:'sfxdhz',
            name: '是否洗涤合作',
            items: [{
                id:1,
                name:'是'
            },{
                id:0,
                name:'否'
            }]
        },{
            id:'dis_code',
            name:'区',
            items:[{
                id:1,
                name:'海淀'
            },{
                id:2,
                name:'朝阳'
            }]
        }]
    };

    init();
}])
.service('FilterService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
    var url = '/crm/crm-hotel-public/';
    return {
        /**
         * 全部区列表
         * @param params
         * @returns {*}
         */
        findList: function (params) {
            var act = 'getDirectList',
                params = {};
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 获取字典列表
         * @param code 字典code
         * @returns {*} 请求
         */
        findDictByCode: function(code) {
            return CrmHttp.post('/crm/crm-visit-record/findValueLabel', {code: code});
        }
    };
}]);